# -*- coding:utf-8 -*-

from tensorflow.core.framework import summary_pb2
import config.glob.config as glob_cfg
import pprint
import logging

"""
日志工具
"""

logging.basicConfig(level=logging.DEBUG, format='%(message)s')


def color_str(msg, fore='def', back='def', style='def'):
    fore = 'lw' if fore == 'def' and back != 'def' else fore
    prefix = '\033['
    # 显示方式
    style_c = {
        'def': '0',  # 默认
        'lig': '1',  # 高亮,key是小写L
        'un': '4',  # 下滑线
        'twi': '5',  # 闪烁
        'rev': '7'  # 反显
    }
    prefix = prefix + style_c[style] + ';'
    # 前景色
    fore_c = {
        'def': '37',  # 普通白
        'lw': '30',  # 亮白
        'r': '31',  # 红
        'ly': '32',  # 亮黄
        'dy': '33',  # 暗黄
        'b': '34',  # 蓝
        'p': '35',  # 紫
        'i': '36',  # 靛蓝
    }
    prefix = prefix + fore_c[fore] + ';'
    # 背景色
    back_c = {
        'def': '',  # 默认黑
        'w': '40',  # 白
        'r': '41',  # 红
        'g': '42',  # 绿
        'y': '43',  # 黄
        'b': '44',  # 蓝
        'p': '45',  # 紫
        'i': '46'  # 靛蓝
    }
    prefix = prefix + back_c[back] + 'm'
    string = prefix + msg + '\033[0m'
    return string


class ClampLog:
    """
    切片日志，with模块包裹
    """
    def __init__(self, msg):
        self.msg = msg

    def __enter__(self):
        if glob_cfg:
            logging.debug(color_str('▼▼Begin ' + self.msg, fore='lw'))

    def __exit__(self, type, value, trace):
        if glob_cfg:
            logging.debug(color_str('▲▲Finish ' + self.msg, fore='lw'))


def log_debug(msg, fore='def', back='def', style='def', form=None,):
    """
    Debug日志封装在此，方便控制
    :param msg:
    :param fore: 前景色
    :param back: 背景色
    :param style: 显示方式
    :param form: 格式
    :return:
    """
    if form == 'yml':
        pp = pprint.PrettyPrinter(indent=1)
        print('\033[36mconfig:')
        pp.pprint(msg)
        print('\033[0m')
    else:
        c_str = '\t' + color_str(msg, fore=fore, back=back, style=style)
        logging.debug(c_str)


def make_summary(name, val):
    """
    记录不在graph中的数据
    :param name:
    :param val:
    :return:
    """
    value = summary_pb2.Summary.Value(tag=name, simple_value=val)
    return summary_pb2.Summary(value=[value])


if __name__ == '__main__':
    print('\033[0;31;43m asad')
